#region Copyright
/*
Copyright Notice: 
Use of this file, in source or binary form, is subject to the terms and conditions set forth 
in the included license.txt file.
*/
#endregion

/* This is a customised template.
 * It overrides some optional methods from EntityMap to allow for testing 
 * with Mocks and fast object creation. Search for #custom# below. 
 */


namespace WebbnessCMS.Security.Models
{

	#region Using statements

	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Data;
	using Neo.Core;
	using Expergent;
	using Expergent.Neo;
	using Castle.Components.Validator;
		using Neo.Core.Util;
		using Neo.Framework;
			
	#endregion

	#region IEntitiesGroup

	/// <summary>
	/// Generated table specific Interface </summary>
	/// <remarks> This Interface is generated by the Neo Framework. Make changes to NeoSupport.vtl.</remarks>
	public partial interface IEntitiesGroup
	{
			/// <summary>
		/// A collection of Permission objects referencing this instance.</summary>
		/// <remarks> Objects referencing this instance.</remarks>
		ObjectRelation<Permission> Permissions { get; }

			
				/// <summary>
		/// Property EntitiesGroupID - No Description </summary>
		/// <remarks> Database Path: WebbnessCMS.Security_EntitiesGroups.EntitiesGroupID.</remarks>
		/// <value> System.Guid. </value>
			System.Guid? EntitiesGroupID {get; set;}
	
					/// <summary>
		/// Property Name - No Description </summary>
		/// <remarks> Database Path: WebbnessCMS.Security_EntitiesGroups.Name.</remarks>
		/// <value> System.String. </value>
			System.String Name {get; set;}
	
		
		}

	#endregion

	#region EntitiesGroup

	/// <summary>
	/// Generated table specific Base Class </summary>
	/// <remarks> This class is generated by the Neo Framework. Make changes to NeoSupport.vtl.</remarks>
	[Serializable]
	public partial class EntitiesGroup : RulesEnabledEntityObject, IEntitiesGroup
	{
			/// <summary>
		/// A collection of Permission objects referencing this instance.</summary>
		/// <remarks> Objects referencing this instance.</remarks>
		public ObjectRelation<Permission> Permissions
		{
			get {return _Permissions;}
		}
		
		private ObjectRelation<Permission> _Permissions;
		
	
			/// <summary>
		/// A collection of ${rel.ForeignEntity.ClassName} objects referencing this instance.</summary>
		/// <remarks> Objects referencing this instance.</remarks>
		public ManyToManyRelation<EntityReference> EntityReference
		{
			get {return _EntityReference;}
		}
		
		private ManyToManyRelation<EntityReference> _EntityReference;
		
			internal EntitiesGroup(System.Data.DataRow aRow, Neo.Core.ObjectContext aContext) : base(aRow, aContext)
		{
				_Permissions = new ObjectRelation<Permission>(this, "Permissions");
					_EntityReference = new ManyToManyRelation<EntityReference>(this, typeof(EntityReferencesToEntitiesGroup), "EntitiesGroup", "EntityReference");
			}
		
				/// <summary>
		/// Property EntitiesGroupID - No Description </summary>
		/// <remarks> Database Path: WebbnessCMS.Security_EntitiesGroups.EntitiesGroupID.</remarks>
		/// <value> System.Guid. </value>
				public virtual System.Guid? EntitiesGroupID
		{
			get { object v = Row["EntitiesGroupID"]; return (System.Guid?)((v != DBNull.Value) ? v : null); }
				set { if (value.HasValue) Row["EntitiesGroupID"] = value; else Row["EntitiesGroupID"] = DBNull.Value; }
				}    

					/// <summary>
		/// Property Name - No Description </summary>
		/// <remarks> Database Path: WebbnessCMS.Security_EntitiesGroups.Name.</remarks>
		/// <value> System.String. </value>
			[ValidateNonEmpty, ValidateLength(0, 128), ValidateIsUniqueInSameTransaction]
				public virtual System.String Name
		{
			get { return Row["Name"] as System.String; }
				set { if (Name == null ) Row["Name"] = (value != null) ? (object)value : (object)DBNull.Value; }
				}    

		
				/// <summary>
		/// Returns the value associated with the propName parameter.</summary>
		/// <remarks> You will need to cast the returned object into its native type upon return.</remarks>
		/// <param name="propName">String property name.</param>
		/// <returns> System.Object. </returns>
		public override object GetProperty(string propName)
		{
					if(propName == "EntitiesGroupID") 
				return EntitiesGroupID;
						if(propName == "Name") 
				return Name;
							if(propName == "Permissions") 
				return Permissions;
				return ExtendGetProperty(propName);
		}
		
		/// <summary>
		/// Sets the value associated with the propName parameter to the value specified.
		/// </summary>
		/// <param name="propName">String property name.</param>
		/// <param name="value">The value.</param>
		public override void SetProperty(string propName, object value)
		{
						if(propName == "EntitiesGroupID") 
			{
				EntitiesGroupID = (System.Guid?)value;
				return;
			}
								if(propName == "Name") 
			{
				Name = (System.String)value;
				return;
			}
							ExtendSetProperty(propName, value);
		}
		
		/// <summary>
		/// Adds a set of facts for this instance to the list.
		/// </summary>
		/// <param name="list">The list.</param>
		/// <param name="parent">The parent.</param>
		protected override void MakeFacts(List<WME> list, string parent)
		{
					list.Add(new WME(new EntityObjectTerm(this), MakePredicate(parent, "EntitiesGroupID"), EntitiesGroupID));
						list.Add(new WME(new EntityObjectTerm(this), MakePredicate(parent, "Name"), Name));
							list.Add(new WME(new EntityObjectTerm(this), MakePredicate(parent, "Permissions"), Permissions));
				ExtendMakeFacts(list, parent);
			myFactsHaveBeenAsserted = true;
		}

		/// <summary>
		/// Gets the default sort order.
		/// </summary>
		/// <value>The sort order.</value>
		public static PropertyComparer DefaultSortOrder
		{
			get
			{
					return new PropertyComparer("Name", SortDirection.Ascending);
				}    
		}

			/// <summary>
		/// Ensures related tables are loaded into memory for a cascaded delete.</summary>
		/// <remarks> Deletes related tables entries when cascade is specified.</remarks>
		public override void Delete() 
		{
						base.Delete();
		}
			
		/// <summary>
		/// Populates the EntityObject from the specified surrogate.
		/// </summary>
		/// <param name="surrogate">The surrogate.</param>
		public void Populate(EntitiesGroupSurrogate surrogate)
		{
							Name = surrogate.Name;
				}
	}

	#endregion

	#region EntitiesGroupTemplate

	/// <summary>
	/// Generated table specific Query Template Class </summary>
	/// <remarks> A Query Template for EntitiesGroup operations. </remarks>
	public class EntitiesGroupTemplate : QueryTemplate
	{

		/// <summary>
		/// Constructor </summary>
		/// <remarks> Public Constructor.</remarks>
		/// <param name="anEntityMap">IEntityMap instance.</param>
		public EntitiesGroupTemplate(IEntityMap anEntityMap) : base(anEntityMap)
		{
		}
		
				/// <summary>
		/// Property EntitiesGroupID - No Description </summary>
		/// <remarks> Database Path: WebbnessCMS.Security_EntitiesGroups.EntitiesGroupID.</remarks>
		/// <value> System.Guid. </value>
		public System.Guid EntitiesGroupID
		{
			get { return (System.Guid)QueryValues["EntitiesGroupID"]; }
			set { QueryValues["EntitiesGroupID"] = value; }
		}

					/// <summary>
		/// Property Name - No Description </summary>
		/// <remarks> Database Path: WebbnessCMS.Security_EntitiesGroups.Name.</remarks>
		/// <value> System.String. </value>
		public System.String Name
		{
			get { return (System.String)QueryValues["Name"]; }
			set { QueryValues["Name"] = value; }
		}

				               
	}

	#endregion

	#region EntitiesGroupFactory

	/// <summary>
	/// Factory class for the creation of EntitiesGroup objects.</summary>
	public class EntitiesGroupFactory : Neo.Framework.GenericObjectFactory<EntitiesGroup>
	{

		/// <summary>
		/// Constructor requiring an ObjectContext parameter.</summary>
		/// <remarks> Main entry point for the retrieval and creation of new EntitiesGroup objects.</remarks>
		/// <param name="context">ObjectContext.</param>
		public EntitiesGroupFactory(ObjectContext context) : base(context, typeof(EntitiesGroup))
		{
		}

			/// <summary>
		/// Create new EntitiesGroup object.</summary>
		/// <remarks>IdMethod = Guid, PrimaryKeyIsForeignKey = False, RelationshipSetsForColumns = 2.</remarks>
		/// <returns> EntitiesGroup </returns>
		public EntitiesGroup CreateObject()
		{
			return (EntitiesGroup)base.CreateObject(null);
		}
		
	
		/// <summary>
		/// Find a specified EntitiesGroup object.</summary>
		/// <remarks> Requires Primary Key values and/or parent object instances.</remarks>
			/// <param name="arg0">.</param>
			/// <returns> EntitiesGroup </returns>
		public EntitiesGroup FindObject(System.Guid arg0)
		{
			return (EntitiesGroup)base.FindObject(new object[] { arg0 } );
		}

		///<summary>
		///Find a specified EntitiesGroup object.</summary>
		///<remarks> Requires Primary Key values and/or parent object instances.</remarks>
			///<param name="arg0">.</param>
			///<returns> EntitiesGroup </returns>
		public EntitiesGroup FindObjectByPK( System.Guid arg0 )
		{
			return (EntitiesGroup) base.FindObject( new object[] {  arg0 } );
		}
		
					
			///<summary>
		///Find a specified EntitiesGroup object.</summary>
		///<remarks> Requires Primary Key values and/or parent object instances.</remarks>
			///<param name="arg0">.</param>
			///<returns> EntitiesGroup </returns>
		public EntitiesGroup FindObjectByPK( System.Guid? arg0 )
		{
			return (EntitiesGroup) base.FindObject( new object[] {  arg0.Value } );
		}
			
		/// <summary>
		/// Return the correct Query Template for the EntitiesGroup object.</summary>
		/// <remarks> A query template is used for creating SQL like queries, in a more OO fashion.</remarks>
		/// <returns> EntitiesGroupTemplate </returns>
		public EntitiesGroupTemplate GetQueryTemplate()
		{
			return new EntitiesGroupTemplate(EntityMap);
		}
		
		/// <summary>
		/// SQL like query to find and return an object.</summary>
		/// <remarks> For example: ...FindFirst("TitleAuthors.(Title = {0} or Title.Publisher = {1})", title, publisher).</remarks>
		/// <param name="qualifierFormat">String.</param>
		/// <param name="parameters">params object[].</param>
		/// <returns> EntitiesGroup. </returns>
		public override EntitiesGroup FindFirst(string qualifierFormat, params object[] parameters)
		{
			ObjectList<EntitiesGroup> results = FindWithLimit(1, qualifierFormat, parameters);
			return (results.Count > 0) ? results[0] : null;
		}

		/// <summary>
		/// SQL like query to find and return an object.</summary>
		/// <remarks> For example: ...FindFirst("TitleAuthors.(Title = {0} or Title.Publisher = {1})", title, publisher).</remarks>
		/// <param name="q">The Qualifier.</param>
		/// <returns>EntitiesGroup</returns>
		public override EntitiesGroup FindFirst(Qualifier q)
		{
			FetchSpecification f = new FetchSpecification(EntityMap, q, 1);
			ObjectList<EntitiesGroup> result = Find(f);
			return result.Count > 0 ? result[0]: null;
		}
	}

	#endregion

	#region EntitiesGroupEntityMap

	//  -------- Entity Map -----------------------------------------------------
	/// <summary>Creates the in memory version of the dataset for the EntitiesGroup DOM.</summary>
	/// <remarks>Generated by Neo Framework.</remarks>
	internal class EntitiesGroupMap : EntityMap
	{
		  /// <summary>Array of Primary Key column names.</summary>
		  /// <remarks>Primary Key columns.</remarks>
		private static readonly string[] pkcolumns = { "EntitiesGroupID" };
		/// <summary>Array of Database column names.</summary>
		  /// <remarks>Database column names.</remarks>
		private static readonly string[] columns = { "EntitiesGroupID", "Name" };
		/// <summary>Array of DOM object properties.</summary>
		  /// <remarks>DOM object properties.</remarks>
		private static readonly string[] attributes = { "EntitiesGroupID", "Name" };
		/// <summary>Array of Related Object names.</summary>
		  /// <remarks>Related Object names.</remarks>
		private static readonly string[] relations = { "EntityReferencesToEntitiesGroups", "Permissions" };

		private Type concreteObjectType = typeof(EntitiesGroup);

		  /// <summary>Property ObjectType.</summary>
		  /// <remarks>Returns the DOM's Type.</remarks>
		  /// <value>System.Type.</value>
		public override System.Type ObjectType
		{
			get { return typeof(EntitiesGroup); }
		}
	    
		/// <summary>Property ObjectType.</summary>
		  /// <remarks>Returns the DOM's Type.</remarks>
		  /// <value>System.Type.</value>
		public override System.Type ConcreteObjectType
		{
			get { return concreteObjectType; }										 /* #custom# overridden */
			set { concreteObjectType = value; Factory.AddCustomType(value, this); }  /* #custom# overridden */
		}
	    
		/// <summary>Property TableName.</summary>
		  /// <remarks>Returns database table name.</remarks>
		  /// <value>string.</value>
		public override string TableName
		{
			get { return "Security_EntitiesGroups"; }
		}
	    
		/// <summary>Property PrimaryKeyColumns.</summary>
		/// <remarks>Array of Primary Key column names.</remarks>
		/// <value>string[].</value>
		public override string[] PrimaryKeyColumns
		{
			get { return pkcolumns; }
		}

		/// <summary>Property Columns.</summary>
		/// <remarks>Array of Database column names.</remarks>
		/// <value>string[].</value>
		public override string[] Columns
		{
			get { return columns; }
		}

		/// <summary>Property Attributes.</summary>
		/// <remarks>Array of DOM object properties.</remarks>
		/// <value>string[].</value>
		public override string[] Attributes
		{
			get { return attributes; }
		}

		/// <summary>Property Relations.</summary>
		/// <remarks>Array of Related Object names.</remarks>
		/// <value>string[].</value>
		public override string[] Relations
		{
			get { return relations; }
		}

		/// <summary>Generates a Hashtable, keyed by Object Name, or information regarding related objects.</summary>
		/// <remarks>Related Objects.</remarks>
		/// <returns>IDictionary.</returns>
  		protected override IDictionary GetRelationInfos()
		{
    		IDictionary infos = new Hashtable(2);
	 	 			infos.Add("EntityReferencesToEntitiesGroups", new RelationInfo(Factory, typeof(EntitiesGroup), typeof(EntityReferencesToEntitiesGroup), "EntitiesGroupID", "EntitiesGroupID"));
			 			infos.Add("Permissions", new RelationInfo(Factory, typeof(EntitiesGroup), typeof(Permission), "EntitiesGroupID", "EntitiesGroupID"));
					return infos;    	
		}
	    
		/// <summary>Based upon the model specified Id Method, returns the proper PkInitializer.</summary>
		/// <remarks>The object used to create a new PK value.</remarks>
		/// <returns>IPkInitializer.</returns>
		public override IPkInitializer GetPkInitializer()
		{
				return new GuidPkInitializer();
			}
	    
		/// <summary>Creates a new instance of EntitiesGroup.</summary>
		/// <remarks>Create new EntitiesGroup.</remarks>
		/// <param name="row">DataRow.</param>
		/// <param name="context">ObjectContext.</param>
		/// <returns>IEntityObject.</returns>
		public override IEntityObject CreateInstance(DataRow row, ObjectContext context) /* #custom# overridden */
		{
			if(ConcreteObjectType != ObjectType)
				return base.CreateInstance(row, context);
			return new EntitiesGroup(row, context);
		}
	    
		/// <summary>Generates the in memory dataset.</summary>
		/// <remarks>Called by the Factory object.</remarks>
		/// <param name="table">DataTable.</param>
		protected override void WriteBasicSchema(DataTable table)
		{
			DataColumn		c;
			
				c = table.Columns.Add("EntitiesGroupID", typeof(System.Guid));
					c.Unique = true;
										c = table.Columns.Add("Name", typeof(System.String));
								table.PrimaryKey = new DataColumn[] { table.Columns["EntitiesGroupID"] };
		}
		
			/// <summary>Generates the dataset relationships.</summary>
		/// <remarks>Dataset Relationships.</remarks>
		/// <param name="table">DataTable.</param>
		protected override void WriteRelations(DataTable table)
		{
			DataRelation r;
			
		 			if(table.DataSet.Relations["Security_EntitiesGroups*Security_EntityReferencesToEntitiesGroups.EntitiesGroupID"] == null)
			{
				r = table.DataSet.Relations.Add("Security_EntitiesGroups*Security_EntityReferencesToEntitiesGroups.EntitiesGroupID", 
						table.DataSet.Tables["Security_EntitiesGroups"].Columns["EntitiesGroupID"],
						table.DataSet.Tables["Security_EntityReferencesToEntitiesGroups"].Columns["EntitiesGroupID"]);
				r.ChildKeyConstraint.UpdateRule = Rule.None;
				r.ChildKeyConstraint.DeleteRule = Rule.None;
			}
			 			if(table.DataSet.Relations["Security_EntitiesGroups*Security_Permissions.EntitiesGroupID"] == null)
			{
				r = table.DataSet.Relations.Add("Security_EntitiesGroups*Security_Permissions.EntitiesGroupID", 
						table.DataSet.Tables["Security_EntitiesGroups"].Columns["EntitiesGroupID"],
						table.DataSet.Tables["Security_Permissions"].Columns["EntitiesGroupID"]);
				r.ChildKeyConstraint.UpdateRule = Rule.None;
				r.ChildKeyConstraint.DeleteRule = Rule.None;
			}
				}
		}

	#endregion

	#region EntitiesGroupSurrogate

	/// <summary>Represents an entity object disconnected from the database.</summary>
	/// <remarks>
	///   <para>Database Path: WebbnessCMS.Security_EntitiesGroups.</para>
	/// </remarks>
	public partial class EntitiesGroupSurrogate : EntityObjectSurrogate, IEntitiesGroup
	{
		/// <summary>Default constructor.</summary>
		public EntitiesGroupSurrogate()
		{
							}
	    
	    
	    
		/// <summary>
		/// Initializes a new instance of the <see cref="EntitiesGroupSurrogate"/> class.
		/// </summary>
		/// <param name="entity">The entity.</param>
		public EntitiesGroupSurrogate(EntitiesGroup entity)
		{
					EntitiesGroupID = entity.EntitiesGroupID;
						Name = entity.Name;
				}

		/// <summary>
		/// A collection of Permission objects referencing this instance.</summary>
		/// <remarks> Objects referencing this instance.</remarks>
		public ObjectRelation<Permission> Permissions
		{
			get {return _Permissions;}
			set {_Permissions = value;}
		}
		
		private ObjectRelation<Permission> _Permissions;

	
	
		/// <summary>
		/// Updates the entity with the values contained within the surrogate.
		/// </summary>
		/// <param name="entity">The entity.</param>
		public void BindObjectInstance(EntitiesGroup entity)
		{
							entity.Name = Name;
				}

					/// <summary>
		/// Property EntitiesGroupID - No Description </summary>
		/// <remarks> Database Path: WebbnessCMS.Security_EntitiesGroups.EntitiesGroupID.</remarks>
		/// <value> System.Guid. </value>
			public System.Guid? EntitiesGroupID
		{
    		get {return _entitiesgroupid;}
				set {_entitiesgroupid = value;}
			}

		private System.Guid? _entitiesgroupid;
		
							/// <summary>
		/// Property Name - No Description </summary>
		/// <remarks> Database Path: WebbnessCMS.Security_EntitiesGroups.Name.</remarks>
		/// <value> System.String. </value>
			[ValidateNonEmpty, ValidateLength(0, 128), ValidateIsUniqueInSameTransaction]
			public System.String Name
		{
    		get {return _name;}
	    		set {_name = value;}
			}

		private System.String _name;
		
			
	
		/// <summary>
		/// Binds the surrogate values to the search template.
		/// </summary>
		/// <param name="emptyTemplate">The empty template.</param>
		public void BindToSearchTemplate(EntitiesGroupTemplate emptyTemplate)
		{
			}

	}
	#endregion

}

